function addOrEditDishDialog(action,id) {
	var params = null;
	var url = website + "boDish/insertPage";
	if(action == ACTION_ADD){
		params = {
			dish_cate_id : id
		};
	}else{
		params = {
			dish_id : id
		};
		url = website + "boDish/updatePage";
	}
	$("#curAction").val(action);
	mask.open(true);
	ajaxGet(url, params, function(result) {
		$("#dialog-form-dish-add").html(result);
		
		var name = $("#name");
		var price = $("#price");
		var price2 = $("#price2");
		var price3 = $("#price3");
		var file = $("#file");
		var desc = $("#desc");
		var allFields = $([]).add(name).add(price).add(price2).add(price3).add(file).add(desc), tips = $(".validateTips");
		$("#dialog-form-dish-add").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					var bValid = true;
					allFields.removeClass("ui-state-error");
					bValid = bValid&& checkLength(name, "名称",1, 16, tips);
					bValid = bValid&& checkLength(price, "价钱",1, 16, tips);

					if(bValid){
						var dish_cate_id = $("#dish_cate_id").val();
						var params = {
							name:name.val(),
							price:price.val(),
							price2:price2.val(),
							price3:price3.val(),
							desc:desc.val(),
							dish_cate_id:dish_cate_id,
							supplierid:$("#curSupplier").val()
						}
						var url = website+"boDish/insert";
						if($("#curAction").val() == ACTION_EDIT){
							url = website+"boDish/update";
							params["id"] = $("#dish_edit_id").val();
						}
						mask.open(true);
						$.ajaxFileUpload
						(
							{
								url:url,
								secureuri:false,
								fileElementId:'file',
								dataType: 'html',
								data:params,
								success: function (data, status)
								{
									if(typeof(data.error) != 'undefined')
									{
										if(data.error != '')
										{
											alert(data.error);
										}else
										{
											alert(data.msg);
										}
									}else{
										$("#dialog-form-dish").html(data);
										initDishBtns(dish_cate_id);
										$("#dialog-form-dish-add").dialog("close");
									}
									mask.close();
								},
								error: function (data, status, e)
								{
									alert(data.responseText);
									mask.close();
								}
							}
						);
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function editDish(obj){
	addOrEditDishDialog(ACTION_EDIT,obj);
}

function showDish() {
	initDish($("#dishCategory").val());
}

function initDish(dish_cate_id) {
	var params = {
		dish_cate_id : dish_cate_id
	}
	mask.open(true);
	ajaxGet(website + "boDish/show", params, function(result) {
		$("#dialog-form-dish").html(result);
		$("#dialog-form-dish").dialog({
			width : 1000,
			height : 650,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					closeDishPage();
				}
			}
		});
		initDishBtns(dish_cate_id);
		mask.close();
	}, "html");
}

function closeDishPage(){
	showDishCategory();
	$("#dialog-form-dish").dialog("close");
	$("#dialog-form-dish").html("");
}

function showDishCategory(){
	initDishCategory($("#curSupplier").val());
}

function initDishCategory(obj) {
	var params = {
		supid : obj
	}
	mask.open(true);
	ajaxGet(website + "boDishCategory/show", params, function(result) {
		$("#dialog-form-dish-category").html(result);
		$("#dialog-form-dish-category").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		$("#create-dish-category-btn").button().click(function() {
			addOrEditDishCateDialog(ACTION_ADD,false);
		});
		$("#edit-dish-category-btn").button().click(function() {
			addOrEditDishCateDialog(ACTION_EDIT,false);
		});
		$("#delete-dish-category-btn").button().click(function() {
			deleteDialog(DISH_CATEGORY, "checkSubKey");
		});
		$("#copy-dish-category-btn").button().click(function() {
			copyDishCates();
		});
		mask.close();
	}, "html");
}

function initDishBtns(dish_cate_id){
	$("#create-dish").button().click(function() {
		addOrEditDishDialog(ACTION_ADD,dish_cate_id);
	});
	$("#delete-dish").button().click(function() {
		deleteDialog(DISH, "checkSubKey");
	});
	$("#copy-dish-btn").button().click(function() {
		copyDishes();
	});
}

function insertDishCategory() {
	var params = {
		name : $("#name").val(),
		supplierid : $("#supplierid").val()
	}
	mask.open(true);
	ajaxPost(website + "boDishCategory/insert", params, function(result) {
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					showDishCategory();
					showSupplier();
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function updateDishCategory(updateId,updateName) {
	var params = {
		id : updateId,
		name : updateName
	}
	mask.open(true);
	ajaxPost(website + "boDishCategory/update", params, function(result) {
		$("#dialog-message").html(result);
		$("#dialog-message").dialog({
			modal : true,
			resizable : false,
			buttons : {
				Ok : function() {
					showDishCategory();
					showSupplier();
					$(this).dialog("close");
				}
			}
		});
		mask.close();
	}, "html");
}

function showDishByCate(obj){
	$("#dishCategory").val(obj);
	initDish(obj);
	$("#dialog-form-dish-category").dialog("close");
}

function addOrEditDishCateDialog(action,isCheck) {
	var ids = "";
	var url = website + "boDishCategory/insertPage";
	var params = null;
	if(action == ACTION_ADD){
		if(!isCheck){
			ids = $("#curSupplier").val();
		}else{
			ids = getSelectedItems("checkKey");
			if (ids != "" && ids.split(",").length != 1) {
				alert("请只选择一个商家");
				return;
			}
		}
	}else{
		ids = $("#curSupplier").val();
	}

	params = {
		supid : ids
	}
	
	$("#curAction").val(action);
	var name = $("#name");
	var allFields = $([]).add(name), tips = $(".validateTips");
	mask.open(true);
	ajaxGet(url, params, function(result) {
		$("#dialog-form-dish-category-add").html(result);
		if(action == ACTION_EDIT){
			$("#name").val($("#dishCategoryName").val());
		}
		$("#dialog-form-dish-category-add").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					var name = $("#name");
					var allFields = $([]).add(name), tips = $(".validateTips");
					var bValid = true;
					allFields.removeClass("ui-state-error");
					bValid = bValid&& checkLength(name, "名称",1, 16, tips);
					if(bValid){
						if(action == ACTION_ADD){
							insertDishCategory();
						}else{
							updateDishCategory($("#dishCategory").val(),name.val());
						}
						$(this).dialog("close");
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function changeDishCate(obj) {
	var dish_cate_id = $(obj).val();
	if (dish_cate_id == "0") {
		return;
	}
	$("#dishCategory").val(dish_cate_id);
	initDish(dish_cate_id);
}


function showSupplierDishCate(obj){
	$("#curSupplier").val(obj);
	initDishCategory(obj);
}

function editDishCate(obj){
	$("#dishCategory").val($(obj).attr("dishCateId"));
	$("#dishCategoryName").val($(obj).attr("dishCateName"));
	addOrEditDishCateDialog(ACTION_EDIT,false);
}

function copyDishCates(){
	var ids = getSelectedItems("checkSubKey");
	if (ids == "") {
		alert("请至少选择一个选项");
		return;
	}
	if($("#combobox").val()==""){
		alert("请选择目标商家");
		return;
	}
	var params = {
		ids:ids,
		targetsupid:$("#combobox").val()
	}
	mask.open(true);
	ajaxPost(website + "boDishCategory/copy", params, function(result) {
		alert("复制成功");
		mask.close();
	}, "html");
}

function copyDishes(){
	var ids = getSelectedItems("checkSubKey");
	if (ids == "") {
		alert("请至少选择一个选项");
		return;
	}
	if($("#dishCategorySelect").val()==null||$("#dishCategorySelect").val()==""){
		alert("请选择目标菜系");
		return;
	}
	var params = {
		ids:ids,
		targetdishcateid:$("#dishCategorySelect").val()
	}
	mask.open(true);
	ajaxPost(website + "boDish/copy", params, function(result) {
		alert("复制成功");
		mask.close();
	}, "html");
}
/*
 * 
 * dish item logic
 */
function addDishItemDialog(dishid){
	$("#curDish").val(dishid);
	addOrEditDishItemDialog(ACTION_ADD);
}

function editDishItemDialog(dishid,dishitemid){
	$("#curDish").val(dishid);
	$("#curDishItem").val(dishitemid);
	addOrEditDishItemDialog(ACTION_EDIT,dishitemid);
}

function addOrEditDishItemDialog(action, updateId) {
	var url = website + "boDishItem/insertOrUpdatePage";
	var params = {
		dishid:$("#curDish").val()
	};
	if(action == ACTION_EDIT){
		params["dishitemid"] = updateId;
	}
	
	$("#curAction").val(action);
	
	mask.open(true);
	ajaxGet(url, params, function(result) { 
		$("#dialog-form-dish-item").html(result);
		$("#dialog-form-dish-item").dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					var size = $("#size");
					var temp = $("#temp");
					var tips = $(".validateTips");
					var bValid = true;
					bValid = bValid&& checkSizeAndTemp(size, temp, tips);
					if(bValid){
						saveDishItem();
						$(this).dialog("close");
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}

function saveDishItem(){
	var params = {};
	if($("#curAction").val()==ACTION_EDIT){
		params = {
			id : $("#curDishItem").val(),
			price : $("#price").val(),
			size : $("#size").val(),
			temp : $("#temp").val(),
			dishid : $("#curDish").val(),
			dishcateid : $("#dishCategory").val(),
			supplierid : $("#curSupplier").val(),
			desc : $("#desc").val()
		};
	}else{
		params = {
			price : $("#price").val(),
			size : $("#size").val(),
			temp : $("#temp").val(),
			dishid : $("#curDish").val(),
			dishcateid : $("#dishCategory").val(),
			supplierid : $("#curSupplier").val(),
			desc : $("#desc").val()
		};	
	}
	
	
	ajaxPost(website + "boDishItem/InsertOrUpdate", params, function(result) {
		$("#dialog-form-dish").html(result);
		$("#dialog-form-dish").dialog({
			width : 1000,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					showDishCategory();
					$(this).dialog("close");
					$(this).html("");
				}
			}
		});
		initDishBtns($("#dishCategory").val());
		mask.close();
	}, "html");
}

function deleteDishItem(id) {
	$("#dialog-confirm").css("display", "inline");
	$("#dialog-confirm").dialog({
		resizable : false,
		height : 140,
		modal : true,
		buttons : {
			"确定删除" : function() {
				var website = $("#website").val();
				var params = {
					ids : id
				}
				mask.open(true);
				ajaxPost(website + "boDishItem/deleteAll", params,
						function(result) {
							$("#dialog-message").html(result);
							$("#dialog-message").dialog({
								modal : true,
								resizable : false,
								buttons : {
									Ok : function() {
										$("#dish-item-"+id).html("");
										$(this).dialog("close");
									}
								}
							});
							mask.close();
						}, "html");
				$(this).dialog("close");
			},
			"取消" : function() {
				$(this).dialog("close");
			}
		}
	});
}
